// pages/room-list/index.js
Page({
  data: {
    roomList: [],
    loading: false,
    empty: false
  },

  onLoad() {
    this.loadRoomList();
  },

  onPullDownRefresh() {
    this.loadRoomList().then(() => {
      wx.stopPullDownRefresh();
    });
  },

  // 加载房间列表
  async loadRoomList() {
    this.setData({ loading: true, empty: false });
    
    try {
      const res = await wx.cloud.callFunction({
        name: 'room',
        data: {
          apiType: 'getRoomList'
        }
      });

      if (res.result.res.code === 200) {
        const roomList = res.result.res.data || [];
        this.setData({
          roomList: roomList,
          loading: false,
          empty: roomList.length === 0
        });
      } else {
        wx.showToast({
          title: res.result.res.msg || '加载失败',
          icon: 'none'
        });
        this.setData({ loading: false });
      }
    } catch (error) {
      console.error('加载房间列表失败:', error);
      wx.showToast({
        title: '加载失败，请重试',
        icon: 'none'
      });
      this.setData({ loading: false });
    }
  },

  // 跳转到房间详情页面
  goToRoomDetail(e) {
    const roomId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/room-detail/index?roomId=${roomId}`
    });
  },

  // 跳转到添加房间页面
  goToAddRoom() {
    wx.navigateTo({
      url: '/pages/room-detail/index'
    });
  }
});

